const app = getApp();
Page({
  data: {
    isHidden: true,
    msg: '正在加载...',
    pageIndex: 1,
    pageTotal: 0,
    tasksList: [],
    typeID: 0,
    sortingList: [
      { key: 1, value: "最新发布", param: 'isNewest' },
      { key: 2, value: "价格最低", param: 'isLowPrice' },
      { key: 3, value: "价格最高", param: 'isHighPrice' }
    ],
    taskTypeList: [

    ],
    current: false,
    upIcon: "../../../images/public/down_@2x.png",
    downIcon: "../../../images/public/down_@2x.png",
    rightIcon: "../../../images/public/choose_@2x.png",
    chioceSorting: false,
    chioceType: false,
    activeTypeIndex: 0,
    activeSortingIndex: 0
  },
  onPullDownRefresh: function () {
    console.log('onPullDownRefresh');
    this.setData({
      pageIndex: 1,
      tasksList: []
    });
    this.getTasksList();
    wx.stopPullDownRefresh()
  },
  onReachBottom: function () {
    console.log('onReachBottom');
    var _this = this;

    _this.setData({
      isHidden: false
    });

    if (_this.data.pageIndex >= _this.data.pageTotal) {
      console.log('completed');
      setTimeout(function(){
        _this.setData({
          isHidden: true,
          msg: '达到底部'
        });
      },1000);
    }else {
      setTimeout(function () {
        console.log('loading');
        _this.data.pageIndex++;
        _this.getTasksList();
      }, 1000);
    }

  },
  //条件选择
  choiceItem: function (e) {
    switch (e.currentTarget.dataset.item) {
      case "1":
        if (this.data.chioceType) {
          this.setData({
            upIcon: "../../../images/public/down_@2x.png",
            downIcon: "../../../images/public/down_@2x.png",
            chioceType: false,
            chioceSorting: false,
          });
        }
        else {
          this.setData({
            upIcon: "../../../images/public/up_@2x.png",
            downIcon: "../../../images/public/down_@2x.png",
            chioceType: true,
            chioceSorting: false,
          });
        }
        break;
      case "2":
        if (this.data.chioceSorting) {
          this.setData({
            upIcon: "../../../images/public/down_@2x.png",
            downIcon: "../../../images/public/down_@2x.png",
            chioceType: false,
            chioceSorting: false,
          });
        }
        else {
          this.setData({
            upIcon: "../../../images/public/down_@2x.png",
            downIcon: "../../../images/public/up_@2x.png",
            chioceType: false,
            chioceSorting: true,
          });
        }
        break;
    }
  },
  hideAllChioce: function () {
    this.setData({
      upIcon: "../../../images/public/down_@2x.png",
      downIcon: "../../../images/public/down_@2x.png",
      chioceType: false,
      chioceSorting: false
    });
  },
  //综合排序
  selectSorting: function (e) {
    var index = e.currentTarget.dataset.index;
    this.setData({
      downIcon: "../../../images/public/down_@2x.png",
      chioceSorting: false,
      activeSortingIndex: index,
      tasksList: [],
      pageIndex: 1
    })
    this.getTasksList();
  },
  taskTypeSorting: function (e) {
    var index = e.currentTarget.dataset.index;
    this.setData({
      upIcon: "../../../images/public/down_@2x.png",
      chioceType: false,
      activeTypeIndex: index,
      tasksList: [],
      pageIndex: 1
    })
    this.getTasksList();
  },
  tasktap: function (e) {
    wx.navigateTo({
      url: '../task_detail/task_detail?id=' + e.currentTarget.dataset.identify,
    })
  },

  onLoad: function (options) {
    var _this = this;
    _this.data.taskTypeList = [];
    _this.data.taskTypeList.push({ key: 0, name: "全部任务" });

    wx.request({
      url: app.globalData.apiDomain + "crowdsource/lookup/getProjectTypeList",
      header: { 'Content-Type': 'application/json' },
      success: function (res) {
        if (res.data.status === 'ok') {
          var list = res.data.object;

          for (var i = 0; i < list.length; i++) {
            _this.data.taskTypeList.push({
              key: list[i].id,
              name: list[i].displayname
            });
          }
          _this.setData({
            taskTypeList: _this.data.taskTypeList
          });

        } else {
          wx.showModal({
            title: '获取任务类型失败',
            showCancel: false,
            content: '失败信息:' + res.data.errorMsg + '    请重试!',
            success: function (res) {
              if (res.confirm) {
              }
            }
          })
        }
      },
      fail: function (e) {
        console.log('tasklist fail');
        wx.showModal({
          title: '获取任务类型失败',
          showCancel: false,
          content: e,
          success: function (res) {
            if (res.confirm) {
            }
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function (options) {
    // 初始化
    this.setData({
      pageIndex: 1,
      tasksList: []
    });
    this.getTasksList();

  },

  getTasksList: function() {
    var _this = this;
    var data = {};
   
    var typeId = _this.data.taskTypeList[_this.data.activeTypeIndex].key;
    console.log('类型id:' + _this.data.taskTypeList[_this.data.activeTypeIndex].key);
    console.log('排序id:' + _this.data.sortingList[_this.data.activeSortingIndex].key);
    switch (_this.data.sortingList[_this.data.activeSortingIndex].key) {
      case 1 :
        data.isNewest = true;
        break;
      case 2 :
        data.isLowPrice = true;
        break;
      case 3 :
        data.isHighPrice = true;
        break;
      default: 
        data.isNewest = true;
        break;
    }
    data.projectTypeId = typeId;
    data.status = 3;
    data.pageNo = _this.data.pageIndex;

    wx.request({
      method: 'POST',
      url: app.globalData.apiDomain + 'crowdsource/project/getAcceptableProjects',
      data: data,
      header: { 'Content-Type': 'application/json' },
      success: function (res) {
        if (res.data.status === 'ok') {
          console.log(res);
          var task = res.data.object;
          console.log(task);
          if (task.pages >= _this.data.pageIndex) {
            _this.data.tasksList = _this.data.tasksList.concat(task.list);
            _this.data.pageTotal = task.pages;
            _this.setData({
              isHidden: true,
              msg: '正在加载...',
              tasksList: _this.data.tasksList
            });
          }
        }
      }
    })
  },

  formatEndDate: function (e) {
    console.log(e);
    var d = new Date(e);
    console.log(d);
    console.log(d.getMonth());
    return d.getMonth() + 1 + '.' + d.getDate();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  }
})